//
//  SetPasswordViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/8/20.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "SetPasswordViewController.h"

#import "SetPersonInfoViewController.h"

#import "UIImage+GradientColor.h"

@interface SetPasswordViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *passwordField;
@property (nonatomic,strong) UITextField *pswField;
@property (nonatomic,strong) UIButton *nextBtn;

@end

@implementation SetPasswordViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)setUI {
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, ScreenX375(44), ScreenX375(44))];
    [backBtn setImage:ImageWithName(@"返回") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(81)) / 2, STATUS_BAR_HEIGHT + ScreenX375(22), ScreenX375(81), ScreenX375(80))];
    logo.image = ImageWithName(@"虾拼 logo");
    [self.view addSubview:logo];
    
    UIImageView *logoNote = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(214)) / 2, ScreenX375(112) + STATUS_BAR_HEIGHT, ScreenX375(214), ScreenX375(14))];
    logoNote.image = ImageWithName(@"标语");
    [self.view addSubview:logoNote];
    
    UIView *passwordLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(30), ScreenX375(37))];
    UIImageView *passwordImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, ScreenX375(9), ScreenX375(14), ScreenX375(18))];
    passwordImg.image = ImageWithName(@"密码");
    [passwordLeft addSubview:passwordImg];
    
    self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(37), ScreenX375(218) + STATUS_BAR_HEIGHT, SCREEN_WIDTH - ScreenX375(74), ScreenX375(37))];
    self.passwordField.font = Kfont(17);
    self.passwordField.placeholder = @"请输入密码";
    self.passwordField.leftView = passwordLeft;
    self.passwordField.clearsOnBeginEditing = NO;
    self.passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordField.leftViewMode = UITextFieldViewModeAlways;
    self.passwordField.secureTextEntry = YES;
    self.passwordField.returnKeyType = UIReturnKeyDone;
    self.passwordField.delegate = self;
    [self.passwordField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.passwordField];
    
    UIView *pswLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(30), ScreenX375(37))];
    UIImageView *pswImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, ScreenX375(9), ScreenX375(14), ScreenX375(18))];
    pswImg.image = ImageWithName(@"密码");
    [pswLeft addSubview:pswImg];
    
    self.pswField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(37), ScreenX375(287) + STATUS_BAR_HEIGHT, SCREEN_WIDTH - ScreenX375(74), ScreenX375(37))];
    self.pswField.font = Kfont(17);
    self.pswField.placeholder = @"请再次输入密码";
    self.pswField.leftView = pswLeft;
    self.pswField.clearsOnBeginEditing = NO;
    self.pswField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.pswField.leftViewMode = UITextFieldViewModeAlways;
    self.pswField.secureTextEntry = YES;
    self.pswField.returnKeyType = UIReturnKeyDone;
    self.pswField.delegate = self;
    [self.pswField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.pswField];
    
    for (int i = 0; i < 2; i++) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(37), ScreenX375(255) + STATUS_BAR_HEIGHT + i * ScreenX375(68), SCREEN_WIDTH - ScreenX375(74), 0.5)];
        line.backgroundColor = LineColor;
        [self.view addSubview:line];
    }
    
    self.nextBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(193)) / 2, ScreenX375(400) + STATUS_BAR_HEIGHT, ScreenX375(193), ScreenX375(103))];
    [self.nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    self.nextBtn.titleLabel.font = Kfont(18);
    [self.nextBtn setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateNormal];
    self.nextBtn.titleEdgeInsets = UIEdgeInsetsMake(-5, 0, 5, 0);
    [self.nextBtn setBackgroundImage:ImageWithName(@"按钮1") forState:UIControlStateNormal];
    [self.nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextBtn];
}


- (void)changeText:(UITextField *)textField {
    if (self.passwordField.text.length > 0 && [self.pswField.text isEqualToString:self.passwordField.text]) {
        [self.nextBtn setBackgroundImage:ImageWithName(@"按钮2") forState:UIControlStateNormal];
    }else {
        [self.nextBtn setBackgroundImage:ImageWithName(@"按钮1") forState:UIControlStateNormal];
    }
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nextClick {
    if (self.passwordField.text.length == 0) {
        [HttpManager showNoteMsg:@"请输入密码"];
        return;
    }
    if (![self.pswField.text isEqualToString:self.passwordField.text]) {
        [HttpManager showNoteMsg:@"两次输入的密码不一致"];
        return;
    }
    SetPersonInfoViewController *vc = [[SetPersonInfoViewController alloc] init];
    vc.phone = _phone;
    vc.password = self.passwordField.text;
    if (_openId) {
        vc.openId = _openId;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
